<?php
/**
 * 小程序首页弹屏广告配置
 * @Author   jiating
 */
namespace app\admin\controller;

use \think\Request;
use app\common\BaseHelper;

use app\admin\model\Tenant;
use app\admin\model\Popon;

class Wxpop extends Base{

    public function index() {
        $request = Request::instance();
        $param = $request->param();
        $tenantId = session('TENANT_ID');
        $store = model('store')->where(['tenantId'=>$tenantId])->order('createDate')->select();
        if(empty($store)) {
            return $this->error('请先添加门店','admin/store/index',2);
        }

        $row = Popon::where(['tenantId'=>$tenantId])->find();
        if(!$row) 
            $row = ['status'=>2,'linkType'=>0];
        $this->assign('row',$row);
        return $this->fetch();
    }

    public function save() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;
        $id = $param['id'];
        if($id) {  //编辑
            $param['updateDate'] = date('Y-m-d H:i:s',time());
            $param['updateOper'] = session('member')['id'];
            $upd = Popon::where('id',$param['id'])->update($param);
            if($upd)
                return json(['code'=>'1','message'=>'保存成功']);
            return json(['code'=>'2','message'=>'出错，请重试']);
        }
        unset($param['id']);
        $auto = [
            'id' => BaseHelper::getUUID(),
            'createDate' => date('Y-m-d H:i:s',time()),
            'updateDate' => date('Y-m-d H:i:s',time()),
            'createOper' => session('member')['id'],
            'updateOper' => session('member')['id'],
            'tenantId' => session('TENANT_ID')
        ];
        $data = array_merge($auto,$param);
        $save = Popon::insert($data);
        if($save)
            return json(['code'=>'1','message'=>'保存成功','id'=>$id]);
        return json(['code'=>'2','message'=>'出错，请重试']);
    }
}